package views.screens.nightScreens;

import controller.human.Person;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import views.MafiaFrame;
import views.panes.ButtonUser;
import views.panes.ButtonUserDead;
import views.screens.Screen;

/**
 *
 * @author mada
 */
class nightScreen2 extends JPanel implements Screen{

    int nrRows, nrCols;
    MafiaFrame mafiaFrame;
    nightScreen1 ns1;
    String action1;

    public nightScreen2(final MafiaFrame mf, String action, final nightScreen1 ns1) {
        super(new GridLayout(getNrRows(mf.players.size())+3, getNrCols(mf.players.size())));

        this.mafiaFrame = mf;
        this.ns1 = ns1;
        this.action1 = action;
        nrRows = getNrRows(mf.players.size())+3;
        nrCols = getNrCols(mf.players.size());

        add(new JLabel("Chose your target: "));
        for (int i = 1; i < nrCols; i++) {
            add(new Component() {
            });
        }
        Iterator<Person> it = mf.players.iterator();
        while(it.hasNext()){
            Person p = it.next();
            JPanel button = null;
            switch(p.getPhysicalState()){
                case Dead:
                    button = new ButtonUserDead(p.getName());
                    break;
                default:
                    button = new ButtonUser(p.getName(), this);
            }
            add(button);
        }

        for(int i=1; i<nrCols;i++)
        add(new Component() {});
        JPanel jp = new JPanel();
        JButton cancel = new JButton("Back");

        jp.add(cancel);
        add(jp);

        cancel.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                ns1.voted.remove(ns1.voted.size()-1);
                mf.update(ns1);
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
    }
     public static int getNrCols(int nrPlayers){
        if(nrPlayers<9)
            return 3;
        if(nrPlayers<16)
            return 4;
        return 5;
    }

    public static int getNrRows(int nrPlayers){
        int nrCols = getNrCols(nrPlayers);
        return nrPlayers/nrCols;
    }

    @Override
    public void setAction(String action) {
        mafiaFrame.update(new nightScreen3(mafiaFrame, action1, action, ns1));
    }

}
